//
//  HCGrowthAlbumView.m
//  HappyChildren
//
//  Created by WillyZhao on 2017/1/19.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCGrowthAlbumView.h"

#import "HCGrowthAlbumHeaderView.h"

#import "HCGrowthAlbumTableViewCell.h"

#import "HCGrowAlumbModel.h"

#import "HJPhotoBrowser.h"


@interface HCGrowthAlbumView () <UITableViewDelegate,HJPhotoBrowserDelegate,UITableViewDataSource,HCGrowthAlbumTableViewCellDelegate> {
    UITableView *tableView_;
    HCGrowAlumbModel *model_;
    HCGrowthAlbumHeaderView *headerView;
    HCGrowAlumbData *currentData;
    HCPopLoadingView *poploading;
}

@end

@implementation HCGrowthAlbumView

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initializeSubViews:frame];
    }
    return self;
}

-(void)initializeSubViews:(CGRect)frame {
    
    //初始化tableView
    tableView_ = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    tableView_.delegate = self;
    tableView_.dataSource = self;
    tableView_.rowHeight = YBKAdaptiveW(105);
    tableView_.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView_.showsVerticalScrollIndicator = NO;
    [self addSubview:tableView_];
    
    
    headerView = [[HCGrowthAlbumHeaderView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, YBKAdaptiveH(300))];
    headerView.backgroundColor = [UIColor whiteColor];
    tableView_.tableHeaderView = headerView;
    
    //用户头像点击事件
    __weak typeof (self)weakSelf = self;
    [headerView setDidClickHeadBlock:^{
        
        if (weakSelf.clickUserHeadBlock) {
            weakSelf.clickUserHeadBlock();
        }
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (![PublicFunction isNull:((HCGrowAlumbData *)(model_.grow[0])).time_record]) {
        return model_.grow.count+1;
    }else{
        return 1;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *iden = [NSString stringWithFormat:@"growthalbum%ld%ld",indexPath.section,indexPath.row];
    [tableView_ registerNib:[UINib nibWithNibName:@"HCGrowthAlbumTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:iden];
    
    HCGrowthAlbumTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden forIndexPath:indexPath];
//    if (!cell) {
//
//        cell = [[HCGrowthAlbumTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"growthalbum"];
//    }
    
    
    [cell cellWithModel:nil indexPath:indexPath];
    cell.delegate = self;
    cell.headIMG.backgroundColor = [UIColor whiteColor];
    cell.cellIndexpath = indexPath;
    if (![PublicFunction isNull:((HCGrowAlumbData *)(model_.grow[0])).time_record]) {
        if (indexPath.row != 0) {
            HCGrowAlumbData *data = model_.grow[indexPath.row - 1];
//            [cell setModel:data loadingView:poploading andCount:model_.grow.count];
            cell.model = data;
        }
    }
    if (indexPath.row>0) {
        
        __weak typeof (self)weakSelf = self;
        [cell setClickHeadimageBlock:^(NSArray *images, NSString *timeText, NSString *contents){
            
            if (weakSelf.clickheadImageBlock) {
                
                weakSelf.clickheadImageBlock(images, timeText, contents);
            }
        }];
    }
    
    return cell;
}

-(void)cellTap {
    

}

#pragma mark  HJPhotoBrowserDelegate

// 返回临时占位图片（即原来的小图）
- (UIImage *)photoBrowser:(HJPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    // 不建议用此种方式获取小图，这里只是为了简单实现展示而已
    NSInteger inde = [model_.grow indexOfObject:currentData];
    NSIndexPath *inddpath = [NSIndexPath indexPathForRow:inde inSection:0];
    HCGrowthAlbumTableViewCell *cell = [tableView_ cellForRowAtIndexPath:inddpath];
    return cell.headIMG.image;
}


// 返回高质量图片的url
- (NSURL *)photoBrowser:(HJPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
{
    
    NSString *urlStr = [NSString stringWithFormat:@"%@",currentData.pic[index]];
    return [NSURL URLWithString:urlStr];
}


-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

-(void)pushController {
    [self.delegate pushControllerd];
}

-(void)setModel:(id)model {
    _model = model;
    model_ = (HCGrowAlumbModel *)_model;
    NSLog(@"成长相册名称:%@",model_.name);

    headerView.nameLabel.text = [NSString stringWithFormat:@"%@",model_.name];
    
    [headerView.imageViewss sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model_.path]]];
    [tableView_ reloadData];
}

-(void)setModel:(id)model loadingView:(HCPopLoadingView *)loading {
    _model = model;
    model_ = (HCGrowAlumbModel *)_model;
    headerView.nameLabel.text = [NSString stringWithFormat:@"%@",model_.name];
    
    [headerView.imageViewss sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model_.path]]];
    poploading = loading;
    [tableView_ reloadData];

}


@end
